﻿using System;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Configuration;
using MongoDB.Bson;
using MongoDB.Driver;
using Sustainalytics.ReportGenerator.BL;
using System.Collections.Generic;
using System.Threading;
using System.IO;

namespace Sustainalytics.Reporting.BusinessLogic.Tests
{
    [TestClass]
    public class RepositoryCollectionAsyncTests
    {
        private const string ConnectionString = "mongodb://localhost:27018/Tests";
        private IMongoDatabase _database;

        [TestInitialize]
        public void Setup()
        {
            var url = MongoUrl.Create(ConnectionString);

            if (string.IsNullOrWhiteSpace(url.DatabaseName))
                throw new ArgumentException("No database specified in the connection string!");

            var client = new MongoClient(url);
            _database = client.GetDatabase(url.DatabaseName);

            //client.DropDatabaseAsync(url.DatabaseName).Wait();
        }

        [TestCleanup]
        public void Cleanup()
        {
            //var url = MongoUrl.Create(ConnectionString);
            //var client = new MongoClient(url);
            //client.DropDatabaseAsync(url.DatabaseName).Wait();

        }
        
        
        [TestMethod]
        public void SaveTest()
        {
            IRepositoryCollectionAsync<ControversiesReportSection> repository = 
                new RepositoryCollectionAsync<ControversiesReportSection>(ConfigurationManager.ConnectionStrings["WiredTiger"].ConnectionString);

            var sections = new List<ControversiesReportSection>();

            //var pdfDocument = File.ReadAllBytes(@"D:\Reports\ControversiesSection.pdf");

            for (var row = 1; row <= 1000; row++)
            {
                sections.Add(new ControversiesReportSection
                {
                    Id = new Guid(string.Format("{0:00000000}-0000-0000-0000-000000000000", row)),
                    PdfDocument = new Guid(string.Format("{0:00000000}-0000-0000-0000-000000000000", row)).ToByteArray(),
                    //PdfDocument = pdfDocument,
                    OutlinePositions = new List<OutlinePosition>(),
                    PageElementPositions = new List<PageElementPosition>()
                });
            }
            //var cts = new CancellationTokenSource();

            //Parallel.ForEach(sections, async section =>
            //{
            //    await repository.Save(section, section.Id, cts.Token);
            //});

            repository.BulkInsert(sections).Wait();

            //sections.ForEach(async section => 
            //{
            //    await repository.Save(section, section.Id);
            //});

            var collection = _database.GetCollection<ControversiesReportSection>(typeof (ControversiesReportSection).Name);
            var count = collection.CountAsync(new BsonDocument()).Result;

            Assert.IsTrue(sections.Count == count, "The collection of report section has a different number of elements than the source collection!");
        }


    }
}
